package main.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import main.dao.HistoryDB;
import main.model.Book;
import main.view.Main;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class ReturnBook {

    @FXML
    private Button setbacks;

    @FXML
    private Button returnBook;

    @FXML
    private TableView<Book> bookTable;

    @FXML
    private TableColumn<Book, String> title;

    @FXML
    private TableColumn<Book, String> createTime;

    @FXML
    private TableColumn<Book, String> returnTime;

    @FXML
    private Button refreshPage;


    @FXML
    void loadData(ActionEvent event) throws SQLException {
        title.setCellValueFactory(new PropertyValueFactory<>("title"));
        createTime.setCellValueFactory(new PropertyValueFactory<>("createTime"));
        returnTime.setCellValueFactory(new PropertyValueFactory<>("returnTime"));
        ResultSet rst =  new HistoryDB().readDate(Login.momentUser.getUsername());
        ObservableList<Book> data = FXCollections.observableArrayList();
        while(rst.next()) {
            System.out.println(rst.getDate(3));
            Date date = rst.getDate(3);
            data.add(new Book(rst.getString(2), rst.getDate(3)));
            bookTable.setItems(data);
        }
    }

    @FXML
    void returnBook(ActionEvent event) throws SQLException {
        new HistoryDB().writeDate(new HistoryDB().readDate(Login.momentUser.getUsername()),Login.momentUser.getUsername());
    }
    @FXML
    void setbacks(ActionEvent event) throws IOException {
        new Main().initUserMenu();
    }
}
